/*
异常
    Exception
    使用关键字Exception捕获异常，可直接打印，
    执行可能发送异常的方法时需要进行预处理，可以try-catch-finally,finally无论是否异常都会执行。
    也可以throws xxException
    try{
        do something
    }catch(Exception e){
        System.out.println("Error:"+e);
    }finally{
        do something
    }
    自定义异常继承于Exception
    class MyException extends Exception{

    ｝

 */
public class Project {
    static void test(int d){
        int a = 40/d;
    }
    public static void main(String[] args){
        int a = 0;
        System.out.println("尝试执行test->"+a);
        try {
            test(a);
            System.out.println("成功执行");
        }catch (Exception e){
            System.out.println("执行目标异常："+e);
        }finally {
            System.out.println("执行完毕");
        }

        int b = 2;
        System.out.println("尝试执行test->"+b);
        try {
            test(b);
            System.out.println("成功执行");
        }catch (Exception e){
            System.out.println("执行目标异常："+e);
        }finally {
            System.out.println("执行完毕");
        }

        try {
            Demo.compare(256);
        } catch (MyException e) {
            System.out.println(e);
        }


    }
}

class MyException extends Exception{
    private int detail;
    MyException(int detail){
        this.detail = detail;
    }
    public String toString(){
        return "MyException"+"["+detail+"]";
    }
}

class Demo{
    static void compare(int var) throws MyException{
        try {
            if (var > 255)
                throw new MyException(var);
            System.out.println("数据合法："+var);
        }catch (MyException e){
            System.out.println(e);
        }
    }
}
